package com.shwread.android.qysw.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000136.R;
import com.shwread.http.base.Log;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;

import logic.util.Util;
import logic.util.Utils;


/**
 * OrderMenuPaySetPasswordActivity
 *
 * @Description 安全设置——设置支付密码界面
 * @Author zhangdong
 * @Date 2015年7月13日上午11:35:07
 */
public class OrderMenuPaySetPasswordActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener {
    private Context context;
    private TextView tvTitle;
    private ImageView ivShow;
    private EditText etPwd, etPwdAgain;
    private Button btnBack, btnSubmit;
    private boolean Is_SHOW_PWD = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.om_my_wallet_pay_password_none);
        this.context = this;
        initView();
        setListener();
        initData();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                default:
                    break;
            }
        }
    };

    private void setListener() {
        btnBack.setOnClickListener(this);
        ivShow.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        tvTitle.setText("设置支付密码");
        btnBack = (Button) findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);

        etPwd = (EditText) findViewById(R.id.et_om_my_wallet_pay_password_pwd);

        etPwdAgain = (EditText) findViewById(R.id.et_om_my_wallet_pay_password_pwd_again);

        ivShow = (ImageView) findViewById(R.id.iv_om_my_wallet_pay_password_pwd_show);

        btnSubmit = (Button) findViewById(R.id.btn_om_my_wallet_pay_password);
    }

    private void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                Util.hideInputMethodManager(context, etPwd);
                Util.hideInputMethodManager(context, etPwdAgain);
                this.finish();
                break;
            case R.id.iv_om_my_wallet_pay_password_pwd_show:
                clickShowPwd();
                break;
            case R.id.btn_om_my_wallet_pay_password:
                Util.hideInputMethodManager(context, etPwd);
                Util.hideInputMethodManager(context, etPwdAgain);
                String pwd = etPwd.getText().toString().trim();
                String pwdAgain = etPwdAgain.getText().toString().trim();
                if (Util.isEmpty(pwd)) {
                    Utils.showToast(context, "请输入6位支付密码");
                } else if (Util.isEmpty(pwdAgain)) {
                    Utils.showToast(context, "请再次输入支付密码");
                } else if (pwd.length() < 6) {
                    Utils.showToast(context, "密码必须为6位数字");
                } else if (!pwd.equals(pwdAgain)) {
                    Utils.showToast(context, "两次输入的密码不一致");
                } else {
                    NewActionHelp.setPayPassword(context, pwd, new SetPasswordHandler());
                }
                break;
            default:
                break;
        }
    }

    private class SetPasswordHandler
            extends
            ObjectHttpResponseHandler<String> {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            if (statusCode < 1000) {
                Utils.ToastShow(OrderMenuPaySetPasswordActivity.this, "网络错误");
            } else {
                Utils.ToastShow(OrderMenuPaySetPasswordActivity.this, "请求出错");
            }
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String object, String responseString) {
            Log.i(responseString);
            if (object.equals("支付密码设置成功")) {
                Util.showToast(context, "设置成功");
                OrderMenuPaySetPasswordActivity.this.finish();
            }
        }

        @Override
        public Type getType() {
            return new TypeToken<String>() {
            }.getType();
        }
    }

    private void clickShowPwd() {
        if (Is_SHOW_PWD) {
            //切换成密文显示
            Is_SHOW_PWD = false;
            ivShow.setSelected(false);
            etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            etPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //切换成明文显示
            Is_SHOW_PWD = true;
            ivShow.setSelected(true);
            etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            etPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    @Override
    public Handler getUIHandler() {
        return handler;
    }

}
